package com.hyw.cm.server.storage.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyw.cm.common.dto.CEntry;
import com.hyw.cm.server.storage.dao.IConnectionRepository;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "设备与服务绑定关系的接口集合", description = "全局共享，与本地进程无关")
@RestController
@RequestMapping("/connection")
public class ConnectionController {

  @Resource
  private IConnectionRepository connectionRepository;

  @GetMapping("/load/{id}")
  @Operation(summary = "加载本地设备")
  public CEntry<String, Integer> load(@Parameter(description = "设备详情") @PathVariable String id) {
    return connectionRepository.findById(id).orElse(null);
  }

  @GetMapping("/list")
  @Operation(summary = "全集合返回", description = "只用于测试环境")
  public Map<String, CEntry<String, Integer>> listAll() {
    return connectionRepository.findAll();
  }

  @GetMapping("/remove/{id}")
  @Operation(summary = "删除关联关系")
  public Boolean remove(@Parameter(description = "设备编号") @PathVariable String id) {
    connectionRepository.remove(id);
    return Boolean.TRUE;
  }

}
